package com.example.android.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.example.android.R
import com.example.android.extensions.Services
import com.example.android.models.Dorm
import com.example.android.viewmodels.DormViewModel
import kotlinx.android.synthetic.main.fragment_select_bed2.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"



/**
 * @ClassName SelectBed2Fragment
 * @Author Lucas
 * @Date 2022/6/7
 * @Description
 **/
class SelectBed2Fragment : Fragment() {
    // TODO: Rename and change types of parameters

    private lateinit var _view: View
    private val _Model : DormViewModel by activityViewModels()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_select_bed2, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _view = view
        lifecycleScope.launch(Dispatchers.Main) {
            Log.d(toString(), "asd")
            Services.SelectBedService.GetDorm(_Model)

        }

        _Model.getDormId().observe(this, Observer<ArrayList<Dorm>>{ dorm ->
            val adapter: ArrayAdapter<String> //数组 配置器 下拉菜单赋值用
            var dormList2:ArrayList<String> = ArrayList()
            Log.d(toString(), "ggggg")
            dorm.forEach {
                Log.d(toString(), it.dormNumber+" "+it.region)
                dormList2.add(it.region+" "+it.dormNumber)
            }
            dormList2.toArray()
            Log.d(toString(), dormList2.toString())

            adapter = ArrayAdapter<String>(context!!, android.R.layout.simple_spinner_item, dormList2)
            spinner.adapter = adapter

            spinner.setSelection(0)



            var listen = myItemClickListener()


            spinner.onItemSelectedListener = listen




        })



            //spinner.setOnItemSelectedListener()





//将可选内容与ArrayAdapter连接起来
//将可选内容与ArrayAdapter连接起来



       // })

        //val model: SelectBedViewModel by viewModels()

            // update UI





        //spinner适配器
//        val Data = ArrayList<String>()
//        Data.add("1")
//        Data.add("2")
//        val adapter = ArrayAdapter(this,R.layout.fragment_select_bed2,Data)
//        spinner.adapter = adapter
//        spinner.onItemSelectedListener()


//        val stu_name_text = findViewById<TextView>(R.id.stu_name)
//
//        val builder = AlertDialog.Builder(stu_name_text.context)
//            //val builder: AlertDialog.Builder = AlertDialog.Builder(this)
//        builder.setTitle("尊敬的用户")
//        builder.setMessage("你真的要卸载我吗？")

        //val next_button = view.findViewById<Button>(R.id.next)
        pre.setOnClickListener {
            val select_bed_step1 = SelectBedFragment()

            requireActivity().supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(
                    R.anim.fragment_item_select_in,
                    R.anim.fragment_item_select_out,
                    R.anim.fragment_item_back_in,
                    R.anim.fragment_item_back_out,
                    )
                .replace(R.id.content_main_fragment,select_bed_step1)
                .addToBackStack(null)
                .commit()
        }




}
    internal inner class myItemClickListener : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

            val title = parent!!.getItemAtPosition(position)

            val tipText = _view.findViewById<TextView>(R.id.tip)
            tipText.text = title.toString()

            if (parent != null) {
                Log.d(toString(),title.toString())
            }

            val str = title.toString()
            var delimiter = " "

            val parts = str.split(delimiter)
            Log.d(toString(),parts.get(0))
            Log.d(toString(),parts.get(1))



            next1.setOnClickListener {

                val select_bed_step3 = SelectBed3Fragment(parts.get(0),parts.get(1))

                requireActivity().supportFragmentManager
                    .beginTransaction()
                    .setCustomAnimations(
                        R.anim.fragment_item_select_in,
                        R.anim.fragment_item_select_out,
                        R.anim.fragment_item_back_in,
                        R.anim.fragment_item_back_out,
                    )

                    .replace(R.id.content_main_fragment,select_bed_step3)
                    .addToBackStack(null)
                    .commit()
            }





        }


        }

}

